package com.yami.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ProdLevel;
import com.yami.shop.bean.model.ProdNft;
import com.yami.shop.common.constants.CacheNames;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.DawInfoMapper;
import com.yami.shop.service.ProdLevelService;
import com.yami.shop.service.ProdNftService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@AllArgsConstructor
public class ProdNftServiceImpl extends ServiceImpl<DawInfoMapper, ProdNft> implements ProdNftService {

    @Autowired
    private DawInfoMapper dawInfoMapper;
    @Autowired
    private ProdLevelService prodLevelService;

    @Override
    @Cacheable(cacheNames = CacheNames.PRODUCT_NFT_KEY, key = "#prodId")
    public ProdNft getDataByProdId(Long prodId) {
        ProdNft prodNft = getOne(new LambdaQueryWrapper<ProdNft>().eq(ProdNft::getProdId, prodId));
        if (Objects.isNull(prodNft)) {
            throw new YamiShopBindException("yami.productNft.no.exist");
        }
        // 获取稀有度
        ProdLevel prodLevelData = prodLevelService.getById(ObjectUtil.isNull(prodNft.getLevelId()) ? 1L : prodNft.getLevelId());
        prodNft.setProdLevel(prodLevelData);

        return prodNft;
    }

    @Override
    public ProdNft getDataByDenomId(String dawNameId) {
        QueryWrapper<ProdNft> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("denom_id", dawNameId);
        return dawInfoMapper.selectOne(queryWrapper);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.PRODUCT_NFT_KEY, key = "#prodId")
    public void removeProdNftCache(Long prodId) {

    }
}
